<?php
	/**
 	 * System delegations class
 	 */
	class SystemDelegationsController extends Controller
	{
		public $page_title = LANG_DELEGATIONS;
		public $layout = 'main';
		public $module = 'administration';
		public $helpers = array ('html', 'htmlx', 'toolbars', 'grids');
		public $name = 'systemdelegations';
		public $javascript = array('calendar/dateselector', 'calendar/popup_lib');

		/**
		 * Gets delegations list
		 */	
		function index()
		{
			$parameters = $this->request->get;
			$parameters[user_id_from]=$_SESSION[userid];
			$out_parameters = array('num_rows');
			$this->data[$this->name] = $this->controller_model->ListItems($parameters, 'sp_system_delegations_list_paged', $out_parameters);
			$this->data[num_rows] = $out_parameters[num_rows];
			$this->data[user_login] = $_SESSION[userlogin];
		}
		
		/**
		 * Edits existing delegation
		 *
		 * @param bigint $id delegation id
		 */	
		function edit($id)
		{
			$this->data[$this->name] = $this->controller_model->Get($id);
			$this->data[users] =  $this->GetModelObject('users')->ListItems(null,'sp_users_list');
			$this->data[user_roles]	= $this->GetModelObject('usersroles')->ListItems(array('user_id'=>$this->data[$this->name][user_id_from]), 'sp_users_roles_list_for_access');
			$roles = $this->controller_model->ListItems(array('id'=>$id), 'sp_system_delegations_get_roles');
			$this->data[exist_roles] = sizeof($roles);
			foreach ($roles as $key=>$value)
			{						
				$this->data[roles][$key] = $value[role_id]; 
			}
			if (!empty($this->request->post))
			{
				$parametrs = $this->request->post;
				$roles_ = $parametrs[roles];
				unset($parametrs[roles]);
				unset($parametrs[user_login_from]);
				$parametrs[role_id] = '0';
				$valid = 1;
				if (strtotime($parametrs[datetime_from])>strtotime($parametrs[datetime_to]))
				{
					$this->controller_model->validation_errors[datetime_from] = LANG_DATETIME_INTERVAL_IS_NOT_VALID;
					$valid = 0;
				}
				
				
				foreach ($roles_ as $key=>$value)
				{
					$parametrs[role_id] = $parametrs[role_id].','.$value;
				}
				if(($this->data[exist_roles]==0||$parametrs[role_id]!='0')&&$valid!=0&&$this->controller_model->Update($parametrs))
				{
					$this->Redirect($this->name);
				}
				else
				{
					$this->data[$this->name] = $this->request->post;
					$this->data[roles] = $roles_;				
				}
			}
			else
			{

			}	
		}
		
		/**
		 * Adds new delegation
		 *
		 * @param string $type type of delegation, delegate role or url
		 */	
		function add($type)
		{
			$this->data[$this->name][user_id_from]=$_SESSION[userid];
			$this->data[$this->name][type]=$type;
			$this->data[$this->name][user_login_from]=$_SESSION[userlogin];
			$this->data[$this->name][datetime_from] = date('d-m-Y H:i');
			$this->data[$this->name][datetime_to] = date('d-m-Y H:i');

			$this->data[$this->name][url] = $this->NormalizeURL($_SERVER[HTTP_REFERER]);
			
			$this->data[users] =  $this->GetModelObject('users')->ListItems(null,'sp_users_list');
			$this->data[user_roles]	= $this->GetModelObject('usersroles')->ListItems(array('user_id'=>$this->data[$this->name][user_id_from]), 'sp_users_roles_list_for_access');
            if($type=='roles')
			$this->data[exist_roles] = 1;
			elseif ($type=='url')
			$this->data[exist_roles] = 0;
			else $this->Redirect($this->name);
			
			if (!empty($this->request->post))
			{
				
				$parametrs = $this->request->post;
				$roles_ = $parametrs[roles];
				unset($parametrs[roles]);
				unset($parametrs[user_login_from]);
				$parametrs[role_id] = '0';
				$parametrs[user_id_from]=$_SESSION[userid];
				
				if($parametrs[type]=='roles')
				$this->data[exist_roles] = 1;
				elseif ($parametrs[type]=='url')
				$this->data[exist_roles] = 0;
				$valid =1;
				if (strtotime($parametrs[datetime_from])>strtotime($parametrs[datetime_to]))
				{
					$this->controller_model->validation_errors[datetime_from] = LANG_DATETIME_INTERVAL_IS_NOT_VALID;
					$valid = 0;
				}
				
				foreach ($roles_ as $key=>$value)
				{
					$parametrs[role_id] = $parametrs[role_id].','.$value;
				}
				unset($parametrs[type]);
				if(($this->data[exist_roles]==0||$parametrs[role_id]!='0')&&$valid!=0&&$this->controller_model->Update($parametrs))
				{
					$this->Redirect($this->name);
				}
				else
				{
					$this->data[$this->name] = $this->request->post;
					$this->data[roles] = $roles_;				
				}
			}
			else
			{

			}	
		}

		/**
		 * View an existing delegation
		 *
		 * @param bigint $id delegation id
		 */	
		function view($id)
		{
			$this->data[$this->name] = $this->controller_model->Get($id);
			$this->data[roles] = $this->controller_model->ListItems(array('id'=>$id),'sp_system_delegations_get_roles');
		}
	
		/**
		 * Delete an existing delegation
		 *
		 * @param bigint $id delegation id
		 * @param integer $confirm deleting confirmation
		 */	
		function delete($id, $confirm = 0)
		{ 
			if ($confirm)
			{
				$this->controller_model->Delete($id);
				$this->Redirect($this->name);
			}
			else
			{
				$this->layout = "empty";
				$this->data[$this->name][id] = $id;
			}
		}
	}
?>